package com.googlecode.jmicroerp.wicket.administrare;

import com.googlecode.jmicroerp.model.NCategorie;
import com.googlecode.jmicroerp.model.NTipRecipient;
import com.googlecode.jmicroerp.service.GenericService;
import com.googlecode.jmicroerp.wicket.DefaultEditPage;
import java.util.List;
import org.apache.wicket.extensions.markup.html.form.DateTextField;
import org.apache.wicket.extensions.yui.calendar.DatePicker;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

/**
 *
 * @author alex
 */
public class EditareTipRecipientePage extends DefaultEditPage {
    private NTipRecipient tipRecipient;
    @SpringBean(name = "genericService")
    private GenericService<NTipRecipient> genericService;
    @SpringBean(name = "genericService")
    private GenericService<NCategorie> categorieService;
    
    public EditareTipRecipientePage() {
        tipRecipient = new NTipRecipient();
        tipRecipient.setVanzareFranctionata(Boolean.FALSE);
        init();
    }
    
    public EditareTipRecipientePage(Long id) {
        tipRecipient = genericService.getById(NTipRecipient.class, id);
        init();
    }
    
    private void init() {
        Form<NTipRecipient> form = new Form<NTipRecipient>("form");
        form.setModel(new CompoundPropertyModel<NTipRecipient>(tipRecipient));
        
        TextField<String> tfCod = new TextField<String>("cod");
        tfCod.setRequired(true);
        form.add(tfCod);
        
        TextField<String> tfDenumire = new TextField<String>("denumire");
        tfDenumire.setRequired(true);
        form.add(tfDenumire);
        
        TextField<String> tfCapacitate = new TextField<String>("capacitate");
        tfCapacitate.setRequired(true);
        form.add(tfCapacitate);
        
        TextField<String> tfCapacitateAlerta = new TextField<String>("capacitateAlerta");
        tfCapacitateAlerta.setRequired(true);
        form.add(tfCapacitateAlerta);
        
        DateTextField dtfDataInceput = new DateTextField("dataInceput");
        dtfDataInceput.add(new DatePicker());
        form.add(dtfDataInceput);
        
        DateTextField dtfDataSfarsit = new DateTextField("dataSfarsit");
        dtfDataSfarsit.add(new DatePicker());
        form.add(dtfDataSfarsit);
        
        CheckBox ckFractionat = new CheckBox("vanzareFranctionata");
        form.add(ckFractionat);
        
        DropDownChoice<NCategorie> ddcCategorie = new DropDownChoice<NCategorie>("categorie", new LoadableDetachableModel<List<NCategorie>>() {

            @Override
            protected List<NCategorie> load() {
                return categorieService.getList(NCategorie.class, null, null);
            }
        }, new ChoiceRenderer<NCategorie>("denumire", "id"));
        form.add(ddcCategorie);
        
        form.add(new SubmitLink("submit") {

            @Override
            public void onSubmit() {
                if (tipRecipient.getId() == null) {
                    genericService.insert(tipRecipient);
                } else {
                    genericService.update(tipRecipient);
                }
                setResponsePage(VizualizareTipRecipientePage.class);
            }
            
        });
        
        form.add(new Link("renuntare") {

            @Override
            public void onClick() {
                setResponsePage(VizualizareTipRecipientePage.class);
            }
            
        });
        
        add(form);
    }
}

